import { ConfigProvider } from 'antd-mobile'
import { stores } from '@/stores'
import { Provider } from 'mobx-react'
import { history, Redirect } from 'umi'
import { NormalLayout, LoginLayout } from './components'
import locale from 'antd-mobile/es/locales/zh-CN'
import type { IRoute } from 'umi'
import { useEffect } from 'react'
import { initWxShare } from '@/utils/wechatHelper'

/** 获取对应的layout */
const getLayout = (props: IRoute) => {
    const { pathname } = history.location
    const LOGIN_LAYOUT_WHITE_LIST: string[] = ['/login', '/register']
    // const NORMAL_LAYOUT_WHITE_LIST: string[] = ['/404']

    // 重定向
    if (pathname === '/' || !stores.userStore.isLogin) {
        return <Redirect to="/report/entry" />
    }

    // 登录模版 白名单
    if (LOGIN_LAYOUT_WHITE_LIST.includes(pathname)) {
        return <LoginLayout {...props} />
    }

    return <NormalLayout {...props} />
}

export default (props: IRoute) => {
    const pathname = history.location.pathname

    useEffect(() => {
        initWxShare()
    }, [pathname])

    return (
        <Provider {...stores}>
            <ConfigProvider locale={locale}>{getLayout(props)}</ConfigProvider>
        </Provider>
    )
}
